package com.example.task1.ui.fastfood;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.FastFoodBean;
import com.example.task1.ui.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class FastFoodAdapter extends BaseAdapter<FastFoodBean.RowsDTO> {

    private Context context;
    private List<FastFoodBean.RowsDTO> mData = new ArrayList<>();
    private ImageView image;
    private TextView tvName;
    private TextView tvLike;
    private TextView tvCollect;
    private TextView tvIngredients;
    private TextView tvLikeNum;
    private TextView tvCollectNum;

    public FastFoodAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<FastFoodBean.RowsDTO> rows) {
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.fastfoodlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<FastFoodBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, FastFoodBean.RowsDTO date) {
        MyApplication.glide(image, date.getImgUrl());
        tvName.setText(date.getName());
        tvLikeNum.setText(date.getLikeCount() + "人赞");
        tvCollectNum.setText(date.getLikeCount() + "人收藏");
        tvIngredients.setText("配料表：" + date.getIngredients());
        if (!date.isMyLikeState()) {
            tvLike.setVisibility(View.GONE);
        }
        if (!date.isMyCollectionState()) {
            tvCollect.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(context, FastFoodDetailActivity.class, date.getId());
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    private void initView(View view) {
        image = view.findViewById(R.id.image);
        tvName = view.findViewById(R.id.tv_name);
        tvLike = view.findViewById(R.id.tv_like);
        tvCollect = view.findViewById(R.id.tv_collect);
        tvIngredients = view.findViewById(R.id.tv_ingredients);
        tvLikeNum = view.findViewById(R.id.tv_likeNum);
        tvCollectNum = view.findViewById(R.id.tv_collectNum);
    }
}
